﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using System.Windows.Input;
using InHD.dataaccess;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using InHD.commands;
using InHD.Properties;
using InHD.utilities;

namespace InHD.viewmodels
{
    class InvoiceTemplateViewModel: WorkspaceViewModel
    {
        #region Constructor
        public InvoiceTemplateViewModel()
        {
            this._invoiceTemplateRepository = new InvoiceTemplateRepository();
            this._invoiceTemplate = InvoiceTemplate.CreateNewInvoiceTemplate();
            LoadInvoiceTemplateList();
            this.PageContent = new InvoiceTplListViewModel(this._invoiceTemplateRowCollection);
            this.PageContent.RequestClose += this.HandleCloseCurrentPage;
        }
        #endregion

        #region Value Property
        private InvoiceTemplate _invoiceTemplate;
        readonly InvoiceTemplateRepository _invoiceTemplateRepository;
        private WorkspaceViewModel _pageContent;
        private List<InvoiceTemplate> _invoiceTemplateRowCollection = new List<InvoiceTemplate>();
        private int _rowIndex = -1;
        #endregion

        #region Presentation Property

        public WorkspaceViewModel PageContent
        {
            get
            {
                return _pageContent;
            }
            set
            {
                _pageContent = value;
            }
        }

        public override string DisplayName
        {
            get
            {
                try
                {
                    return Resources.Title_Invoice;
                }
                catch (Exception ex)
                {
                    Logger.RecordMessage(ex, MessageType.Error);
                    return string.Empty;
                }
            }
            protected set
            {
                base.DisplayName = value;
            }
        }
        #endregion

        #region Normal Method
        private void LoadInvoiceTemplateList()
        {
            this._invoiceTemplateRepository.LoadAllInvoiceTemplate();
            this._invoiceTemplateRowCollection = this._invoiceTemplateRepository.GetInvoiceTemplate();
        }

        private void HandleCloseCurrentPage(object source, EventArgs e)
        {
            this.PageContent.RequestClose -= this.HandleCloseCurrentPage;
            if (this.PageContent is InvoiceTplListViewModel)
            {
                string commandExcuted = ((InvoiceTplListViewModel)PageContent).commandExcute;
                if (commandExcuted.Equals("View"))
                {  
                    this._invoiceTemplate = ((InvoiceTplListViewModel)PageContent).RowIndexItem;
                    InvoiceTemplate temp = InvoiceTemplate.CreateNewInvoiceTemplate(_invoiceTemplate.Id, _invoiceTemplate.InvoiceUNK, _invoiceTemplate.Content, _invoiceTemplate.Name, _invoiceTemplate.Modified, _invoiceTemplate.ModifiedBy, _invoiceTemplate.TransType, _invoiceTemplate.Code, _invoiceTemplate.RealContent, _invoiceTemplate.ArchiveFlag);
                    this.PageContent = new InvoiceTplFormViewModel(temp);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Add"))
                {
                    _rowIndex = -1;
                    this._invoiceTemplate = InvoiceTemplate.CreateNewInvoiceTemplate();
                    this.PageContent = new InvoiceTplFormViewModel(_invoiceTemplate);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Close"))
                {
                    this.OnRequestClose();
                }
            }
            else if (this.PageContent is InvoiceTplFormViewModel)
            {
                string commandExcuted = ((InvoiceTplFormViewModel)PageContent).commandExcute;
                if (commandExcuted.Equals("Close"))
                {
                    this.PageContent = new InvoiceTplListViewModel(_invoiceTemplateRowCollection);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Save") || commandExcuted.Equals("Delete"))
                {
                    LoadInvoiceTemplateList();
                    this.PageContent = new InvoiceTplListViewModel(_invoiceTemplateRowCollection);
                    this.PageContent.RequestClose += this.HandleCloseCurrentPage;
                    OnPropertyChanged("PageContent");
                }
                else if (commandExcuted.Equals("Edit"))
                {
                    
                }
            }
        }
        #endregion
    }
}
